package com.strange.mdl.module.mdl.controller.app.incense;

import com.strange.mdl.module.mdl.dal.mysql.incense.IncenseMapper;
import jakarta.annotation.security.PermitAll;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;

import java.util.*;

import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.pojo.CommonResult;
import com.strange.mdl.framework.common.util.object.BeanUtils;
import static com.strange.mdl.framework.common.pojo.CommonResult.success;

import com.strange.mdl.module.mdl.controller.app.incense.vo.*;
import com.strange.mdl.module.mdl.dal.dataobject.incense.IncenseDO;
import com.strange.mdl.module.mdl.service.incense.IncenseService;

@Tag(name = "用户 APP - 供香记录")
@RestController
@RequestMapping("/mdl/appincense")
@Validated
public class AppIncenseController {

    @Resource
    private IncenseService incenseService;

    @Resource
    private IncenseMapper incenseMapper;

    @PostMapping("/create")
    @Operation(summary = "创建供香记录")
    @PermitAll
    public CommonResult<Long> createIncense(@Valid @RequestBody AppIncenseSaveReqVO createReqVO) {
        return success(incenseService.appCreateIncense(createReqVO));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供香记录分页")
    @PermitAll
    public CommonResult<PageResult<AppIncenseRespVO>> getIncensePage(@Valid AppIncensePageReqVO pageReqVO) {
        PageResult<IncenseDO> pageResult = incenseService.getAppIncensePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppIncenseRespVO.class));
    }

    @GetMapping("/rankNumber")
    @Operation(summary = "多少用户上供")
    @PermitAll
    public CommonResult<Long> getRankNumber() {
        List<IncenseDO> incenseDOS = incenseMapper.selectList();
        long uniqueUserCount = incenseDOS.stream().filter(item -> item.getRankNumber() != null)
                .map(IncenseDO::getUserId)     // 提取每个对象的 userId
                .distinct()                    // 去重
                .count();                      // 统计数量
        return success(uniqueUserCount);
    }
//
//    @PutMapping("/update")
//    @Operation(summary = "更新供香记录")
//    public CommonResult<Boolean> updateIncense(@Valid @RequestBody AppIncenseSaveReqVO updateReqVO) {
//        incenseService.updateIncense(updateReqVO);
//        return success(true);
//    }
//
//    @DeleteMapping("/delete")
//    @Operation(summary = "删除供香记录")
//    @Parameter(name = "id", description = "编号", required = true)
//    public CommonResult<Boolean> deleteIncense(@RequestParam("id") Long id) {
//        incenseService.deleteIncense(id);
//        return success(true);
//    }
//
//    @DeleteMapping("/delete-list")
//    @Parameter(name = "ids", description = "编号", required = true)
//    @Operation(summary = "批量删除供香记录")
//    public CommonResult<Boolean> deleteIncenseList(@RequestParam("ids") List<Long> ids) {
//        incenseService.deleteIncenseListByIds(ids);
//        return success(true);
//    }
//
//    @GetMapping("/get")
//    @Operation(summary = "获得供香记录")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    public CommonResult<AppIncenseRespVO> getIncense(@RequestParam("id") Long id) {
//        IncenseDO incense = incenseService.getIncense(id);
//        return success(BeanUtils.toBean(incense, AppIncenseRespVO.class));
//    }



}